package com.mobilous.client.project.pagemanager;

import java.util.ArrayList;

import org.vaadin.gwtgraphics.client.DrawingArea;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.mobilous.client.project.BinDropController;
import com.mobilous.client.project.PageOutline;
import com.mobilous.client.project.ThrashIconFile;
import com.mobilous.client.project.pageeditor.PageEditor;
import com.mobilous.client.tooltip.TooltipListener;

public class ApplicationPages extends Composite {
	private ArrayList<PageOutline> pages = new ArrayList<PageOutline>();
	PickupDragController dragController;
	AbsolutePanel absolutePanel;
	static PageName pagenamedialog;
	
	//private ArrayList<LineLink> links = new ArrayList<LineLink>();

	public ApplicationPages() {
		absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("1024px", "722px");
		final DrawingArea canvas = new DrawingArea(1024, 722);
		absolutePanel.add(canvas);
		dragController = new PickupDragController(absolutePanel, true);

		final AbsolutePanel absolutePanelMenuBar = new AbsolutePanel();
		absolutePanelMenuBar.setStyleName("gwt-background-SubMenu");
		absolutePanel.add(absolutePanelMenuBar, 0, 0);
		absolutePanelMenuBar.setSize("1024px", "52px");

		Button btnAddScreen = new Button("Add Screen");
		absolutePanelMenuBar.add(btnAddScreen, 10, 10);

		ThrashIconFile thrashicon = new ThrashIconFile("images/recycle_bin.png", "");
		absolutePanelMenuBar.add(thrashicon, 941, 10);
		thrashicon.setSize("30px", "30px");
		dragController.setBehaviorDragStartSensitivity(1);
		DropController closedTrashBinDropController = new BinDropController(thrashicon);
		dragController.registerDropController(closedTrashBinDropController);
		TooltipListener image_ThrashTip = new TooltipListener("Drag useless screens here to remove.", 5000 ,"gwt-tooltip",thrashicon);
		thrashicon.addMouseOutHandler(image_ThrashTip);
		thrashicon.addMouseOverHandler(image_ThrashTip);


		Image image_PullUp = new Image("images/bullet_arrow_up.png");
		absolutePanelMenuBar.add(image_PullUp, 992, 10);
		image_PullUp.setSize("32px", "32px");
		TooltipListener image_PullUpTip = new TooltipListener("Hide Menu Options.", 5000 ,"gwt-tooltip",image_PullUp);
		image_PullUp.addMouseOutHandler(image_PullUpTip);
		image_PullUp.addMouseOverHandler(image_PullUpTip);

		final Image image_PullDown = new Image("images/bullet_arrow_down.png");
		TooltipListener image_PullDownTip = new TooltipListener("Show Menu Options.", 5000 ,"gwt-tooltip",image_PullDown);
		image_PullDown.addMouseOutHandler(image_PullDownTip);
		image_PullDown.addMouseOverHandler(image_PullDownTip);

		absolutePanel.add(image_PullDown, 992, 10);
		image_PullDown.setSize("32px", "32px");
		image_PullDown.setVisible(false);

		image_PullUp.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				absolutePanelMenuBar.setVisible(false);
				image_PullDown.setVisible(true);
			}
		});

		image_PullDown.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				image_PullDown.setVisible(false);
				absolutePanelMenuBar.setVisible(true);
			}
		});


		
		dragController.addDragHandler(new DragHandler() {

			@Override
			public void onPreviewDragStart(DragStartEvent event)
					throws VetoDragException {
				System.out.println("ZZZZ");

			}

			@Override
			public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {
				System.out.println("ZZZZYY");

			}

			@Override
			public void onDragStart(DragStartEvent event) {
				//canvas.clear();
				System.out.println("DragStarted");
			}

			@Override
			public void onDragEnd(DragEndEvent event) {
				drawlinks();
			}
		});
		
		
		final Image image = new Image("images/start-here-page.png");
		absolutePanel.add(image, 10, 662);
		image.setSize("50px", "50px");
		help();
		btnAddScreen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				PageOutline create = new PageOutline();
				pagenamedialog=new PageName(create,pages,canvas,image);
				pages.add(create);
				pagenamedialog.setAnimationEnabled(true);
				pagenamedialog.center();
				pagenamedialog.show();
				absolutePanel.add(create,100,55);
				dragController.makeDraggable(create);
				create.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						new PageEditor(); 
					}});
			}
		});

		//Timerfix for the widgets to draw on the screen before drawing lines. otherwise expect crash..
		Timer timer = new Timer() {
			public void run() {
				canvas.clear();
				drawlinks(); 
			}
		};
		timer.scheduleRepeating(30);
	}

	void drawlinks()
	{
		for(int i=0;i<pages.size();i++)
		{
			pages.get(i).link.draw();
		}
	}
	
	private DialogBox dialogBox = new DialogBox();
	void help()
	{
		// Create the popup dialog box

		dialogBox.setText("Need Assistance");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>My Apps</b>"));
		dialogVPanel.add(new HTML("This is to help you find your way around this mess."));
		dialogVPanel.add(new HTML("<b>People</b>"));
		dialogVPanel.add(new HTML("This is to help you find your way around this mess."));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.center();
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		dialogBox.hide();
	}
}
